package com.bwie.servicemodule.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.servicemodule.controler.GlideControler;

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> array; // 存放视图的数组
    private Context context; // 上下文
    private GlideControler glideControler; // 加载网络图片
    public RecyclerViewHolder(@NonNull View itemView, Context context) {
        super( itemView );
        array = new SparseArray<>(  );
        this.context = context;
        glideControler = new GlideControler();
    }

    /**
     * msg: 查找控件
     */
    public <T extends View>T f( int id) {
        T t = (T) array.get( id );
        if( t == null)  {
            t = itemView.findViewById( id );
            array.put( id, t );
        }
        return t;
    }

    /**
     * msg：加载图片
     * */
    public RecyclerViewHolder loadMatchImage( int id, String uri) {
        ImageView f = f( id );
        glideControler.loadMatchImg( context, uri, f );
        return this;
    }

    public RecyclerViewHolder loadCircleImage( int id, String uri) {
        ImageView f = f( id );
        glideControler.loadCircleImg( context, uri, f );
        return this;
    }

    public RecyclerViewHolder loadGifImage( int id, String uri) {
        ImageView f = f( id );
        glideControler.loadGifImg( context, uri, f );
        return this;
    }

    public RecyclerViewHolder loadImage( int id, String uri) {
        ImageView f = f( id );
        glideControler.loadImg( context, uri, f );
        return this;
    }

    public RecyclerViewHolder loadRoundImage( int id, String uri) {
        ImageView f = f( id );
        glideControler.loadRoundImg( context, uri, f );
        return this;
    }

    public RecyclerViewHolder loadImage( int id, int resouce) {
        ImageView f = f( id );
        f.setImageResource( resouce );
        return this;
    }

    /**
     * msg：发送文字
     * */
    public RecyclerViewHolder loadText(int id, String contend) {
        TextView f = f( id );
        f.setText( contend );
        return this;
    }
}
